//
//  ViewController.swift
//  JKSQLite
//
//  Created by 王冲 on 2018/4/30.
//  Copyright © 2018年 希艾欧科技有限公司. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
       
        
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        print(#function)
        
        
        
    }
    
    
    // MARK: 预编译优化数据库
    func batchExecSQL() {
        
        let start = CFAbsoluteTimeGetCurrent()
        let manager = JKSQLiteManger.shareManger()
        // 开启事务
        manager.beginTransaction()
        for i in 0..<10000
        {
            let sql = "INSERT INTO T_Person8" +
                "(name, age)" +
                "VALUES" +
            "(?, ?);"
            
            manager.batchExecSQL(sql: sql, args: "yy +\(i)", 1 + i)
        }
        // 提交事务
        manager.commitTransaction()
        print("耗时 = \(CFAbsoluteTimeGetCurrent() - start)")
    }
    
    
    // MARK: 数据的异步插入
    func insertasyncData() {
    
        let person = Person(dict: ["name": "mm" as AnyObject ,"age": 90 as AnyObject])
        // 数据的异步插入
        person.insertQueuePerson()
        
    }
    
    // MARK: 数据的查询
    func loadData() {
        
        print(Person.loadPersons())
        
    }
    
    // MARK: 数据的插入、更新、删除
    func insertUpdateDelete() {
    
        let person = Person(dict: ["name": "mm" as AnyObject ,"age": 90 as AnyObject])
        // 1、增加数据
        print(person.insertPerson())
        // 2、更新数据
        //print(person.updatePerson(name: "wc"))
        // 1、删除数据
        //print(person.deletePerson())
    }
    
    // MARK: 带有事件的SQLite优化
    func transaction() {
        
        let start = CFAbsoluteTimeGetCurrent()
        let manger = JKSQLiteManger.shareManger()
        manger.beginTransaction()
        for i in 0..<10000 {
            
            let p = Person(dict: ["name": "zs + \(i)" as AnyObject, "age": 3 + i as AnyObject])
            p.insertPerson()
        }
        manger.commitTransaction()
        print("耗时 = \(CFAbsoluteTimeGetCurrent() - start)")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }


}

